<?php
/**
 * Created by PhpStorm.
 * User: yulin
 * Date: 2018-7-10
 * Time: 14:27
 */
namespace app\open\model;

use app\open\filter\CarBuyList;
use app\base\exception\RecordNotFoundException;
use think\Db;
use traits\model\SoftDelete;
class CarAudit extends Common
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    /**
     * 车辆购置列表
     * @param string $fields
     * @param array $where
     * @param int $page
     * @param int $num
     * @return array
     */
    public function buyList($fields = '*', $where = [], $page = 1, $num = 10)
    {
        $filter = new CarBuyList();
        $filter->analyzeField();
        list(, $msg) = $filter->checkField($fields);
        if ($where['car_audit_id']) {
            $where2['car_audit_id'] = $where['car_audit_id'];
        }
        if ($where['company_name']) {
            $where2['company_name'] = $where['company_name'];
        }
        if ($where['state']) {
            $where2['state'] = $where['state'];
        }
        list(, $whereMsg) = $filter->checkWhere($where2);
        $whereMsg['a.delete_time'] = ["null"];

        $buildQuery = function ($query) use ($whereMsg) {
            self::authCompanyQuery($query, $whereMsg, 'a.company_id');
        };

        $data['data'] = Db::name('car_audit')
            ->alias('a')
            ->join('company c', 'a.company_id = c.company_id', 'left')
            ->join('car_brand d', 'a.car_brand_id = d.car_brand_id', 'left')
            ->join('car_brand e', 'a.car_series_id = e.car_brand_id', 'left')
            ->join('car_brand f', 'a.car_model_id = f.car_brand_id', 'left')
            ->join('passenger g', 'a.passenger_id = g.passenger_id', 'left')
            ->join('car h', 'a.car_id = h.car_id', 'left')
            ->join('car_type i', 'i.car_type_id = h.car_type_id', 'left')
            ->field($msg)
            ->where($buildQuery)
            ->order('date desc')
            ->page($page, $num)
            ->select();
        $data['count'] = count($data['data']);
//        $data['data']        = $filter->checkData($data['data']);
        $data['description'] = $filter->title;
        $data['fields'] = $filter->fieldTitle;
        return [1, $data];
    }

    /**
     * 新增或者编辑购车申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function buyEdit($params)
    {
        $data = [];

        $where = [];
        $params['car_audit_id'] && $where['car_audit_id'] = $params['car_audit_id'];

        $update = [];
        $params['company_id'] && $update['company_id'] = $params['company_id'];
        $params['passenger_id'] && $update['passenger_id'] = $params['passenger_id'];
        $params['car_brand_id'] && $update['car_brand_id'] = $params['car_brand_id'];
        $params['car_series_id'] && $update['car_series_id'] = $params['car_series_id'];
        $params['car_model_id'] && $update['car_model_id'] = $params['car_model_id'];
        $params['people'] && $update['people'] = $params['people'];
        $params['money'] && $update['money'] = $params['money'];
        $params['buyto'] && $update['buyto'] = $params['buyto'];
        $params['whybuy'] && $update['whybuy'] = $params['whybuy'];
        $params['state'] && $update['state'] = $params['state'];
        $params['car_id'] && $update['car_id'] = $params['car_id'];

        $session = (new User())->getLoginUser();
        if (!self::isSuperAdmin($session) && $session['user']['company_id']) {
            $update['company_id'] = $session['user']['company_id'];
        }

        if ($params['car_audit_id']) {

            if ($update['state'] == '2') {
                $update['date_of_audit'] = date("Y-m-d H:i:s");
                $car_id = Db::name('car_audit')->where($where)->value('car_id');
                Db::name('car')->where('car_id',$car_id)->update(['state' => 6]);
            }
            if ($update['state'] == '3') {
                $update['date_of_audit'] = date("Y-m-d H:i:s");
                $car_id = Db::name('car_audit')->where($where)->value('car_id');
                Db::name('car')->where('car_id',$car_id)->update(['state' => 6]);
            }
            if ($update['state'] == '4') {
                $update['date_of_registration'] = date("Y-m-d H:i:s");
                $car_id = Db::name('car_audit')->where($where)->value('car_id');
                Db::name('car')->where('car_id',$car_id)->update(['state' => 6]);
            }
            if ($update['state'] == '5') {
                $update['date_of_approval'] = date("Y-m-d H:i:s");
                $car_id = Db::name('car_audit')->where($where)->value('car_id');
                Db::name('car')->where('car_id',$car_id)->update(['state' => 6]);
            }
            if ($update['state'] == '6') {
                $update['date_of_last'] = date("Y-m-d H:i:s");
                $car_id = Db::name('car_audit')->where($where)->value('car_id');
                Db::name('car')->where('car_id',$car_id)->update(['state' => 6]);
            }

            $buildQuery = function ($query) use ($where) {
                self::authCompanyQuery($query, $where, 'company_id');
            };

            $data['data'] = Db::name('car_audit')->where($buildQuery)->update($update);
            $data['count'] = $data['data'];

            return [1, $data];
        } else if ($update) {

            $update['audit_no'] = $this->createSn();
            $update['state'] = 1;
            $update['date'] = date("Y-m-d H:i:s");
            $data['data'] = Db::name('car_audit')->insert($update);
            $data['count'] = $data['data'];

            return [1, $data];
        } else {
            return [0, $data];
        }
    }

    /**
     * 删除购车申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function buyDelete($id = '')
    {
        $data = [];
        $data['data'] = Db::name('car_audit')->where('car_audit_id', $id)->update(['delete_time' => time()]);
        $data['count'] = $data['data'];
        if($data['count']) {
            return [1, $data];
        }
        else{
            return [0, $data];
        }
    }
}